package com.wuliupai.delivery.wuliupaidelivery.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import com.wuliupai.delivery.wuliupaidelivery.App;
import com.wuliupai.delivery.wuliupaidelivery.R;
import com.wuliupai.delivery.wuliupaidelivery.activity.LoginActivity_;
import com.wuliupai.delivery.wuliupaidelivery.activity.TaskCurrentListActivity_;
import com.wuliupai.delivery.wuliupaidelivery.bean.BaseBean;
import com.wuliupai.delivery.wuliupaidelivery.ui.RefreshAndLoadLayout;

import org.androidannotations.annotations.EBean;
import org.androidannotations.annotations.RootContext;

import java.util.regex.Pattern;

/**
 * 公共工具类
 */
@EBean(scope = EBean.Scope.Singleton)
public class CommonUtils {
    @RootContext
    Context mContext;

    /**
     * 获取版本信息
     *
     * @return String[0]:版本名称,String[1]版本号
     */
    public static String[] getVersionName() {
        String[] versionInfo = {"", ""};
        try {
            PackageManager packageManager = App.mContext.getPackageManager();
            // getPackageName()是你当前类的包名，0代表是获取版本信息
            PackageInfo packInfo = packageManager.getPackageInfo(App.mContext.getPackageName(), 0);
            versionInfo[0] = packInfo.versionName;
            versionInfo[1] = packInfo.versionCode + "";
        } catch (PackageManager.NameNotFoundException e) {

        }
        return versionInfo;
    }


    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public int dp2px(float dpValue) {
        final float scale = mContext.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
     */
    public int px2dp(float pxValue) {
        final float scale = mContext.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    /**
     * 将px值转换为sp值，保证文字大小不变
     */
    public int px2sp(Context context, float pxValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (pxValue / fontScale + 0.5f);
    }

    /**
     * 将px值转换为dp值，保证尺寸大小不变
     */
    public int px2dp(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    /**
     * 判斷輸入的内容是否是手机号
     *
     * @param input
     * @return
     */
    public boolean isPhoneNumber(String input) {
        String regex = "1([\\d]{10})";
//        String regex="1([\\d]{10})|((\\+[0-9]{2,4})?\\(?[0-9]+\\)?-?)?[0-9]{7,8}";
        Pattern p = Pattern.compile(regex);
        return p.matcher(input).matches();
    }

    /**
     * 将手机号显示成密文
     *
     * @param phoneNumber
     * @return
     */
    public String encryptPhoneNumber(String phoneNumber) {
        if (phoneNumber.length() <= 3) {
            return phoneNumber;
        }
        return phoneNumber.substring(0, 3) + "***";
    }

    /**
     * 从引导页和启动页开启下一个页面
     *
     * @param activity
     */
    public void startNextActivity(Activity activity) {
        BaseBean baseEntity = new BaseBean();
        String loginTag = baseEntity.getUserId();
        try {
            if (loginTag.equals("0") ||
                    loginTag == null ||
                    "".equals(loginTag) ||
                    loginTag.equals("null")) {
                LoginActivity_.intent(activity).start();
                activity.finish();
            } else {
                Intent intent = new Intent(activity, TaskCurrentListActivity_.class);
                activity.startActivity(intent);
                activity.finish();
            }
        } catch (NullPointerException e) {
            LoginActivity_.intent(activity).start();
            activity.finish();
        }
    }
    /**
     * 初始化RefreshAndLoadLayout
     *
     * @param swipe_layout
     */
    public void initRefreshAndLoadLayout(RefreshAndLoadLayout swipe_layout) {
        swipe_layout.setColorSchemeResources(R.color.color_progress_1,
                R.color.color_progress_2,
                R.color.color_progress_3,
                R.color.color_progress_4,
                R.color.color_progress_5,
                R.color.color_progress_6
        );
    }
}
